package com.woniuxy.exam.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import com.woniuxy.commons.entity.*;
import com.woniuxy.exam.entity.PaperDto;

public class PrintUtil {
    private static final String DEST = "target/";
    private static final String FONT = "static/simsun.ttc";


   // public static void main(String[] args) throws DocumentException, IOException {
//		Document document = new Document();
//		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
//		document.open();
//		Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//		document.add(new Paragraph("hello world1 我是你爹",f1));
//		document.close();
//		writer.close();

//        //TODO 书写进试卷serviceimpl中
//        System.out.println("--------------");
//        Document document = createPdf(DEST, new PaperDto());
//        createFile(document, new PaperDto());
//        document.close();
//        System.out.println("==------------");
   // }

    /**
     * 创建一个pdf并打开
     *
     * @param outpath pdf路径
     */
    public static Document createPdf(String outpath, PaperDto paperDto) throws DocumentException, IOException {
        //页面大小
        //Rectangle rect = new Rectangle(PageSize.A4.rotate());//文档横方向
        Rectangle rect = new Rectangle(PageSize.A4);//文档竖方向
        //如果没有则创建
        File saveDir = new File(outpath);
        File dir = saveDir.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Document doc = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outpath));
        //PDF版本(默认1.4)
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
        //文档属性
        //doc.addTitle(paperDto.getName());
        doc.addAuthor("yxc");
        doc.addSubject("Subject");
        doc.addKeywords("testPaper");
        doc.addCreator("yxc");
        //页边空白
        doc.setMargins(40, 40, 40, 40);
        //打开文档
        doc.open();
        return doc;
    }

    public static void createFile(Document doc, PaperDto paperDto) throws DocumentException {
        PaperStructure structure = paperDto.getPaperStructure();
        doc.add(PdfFontUtil.getFont(2, paperDto.getName()));
        doc.add(PdfFontUtil.getFont(6, "考试时间：" + paperDto.getTime() + "     总分：" + structure.getQuestionTotalScore()));
        doc.add(PdfFontUtil.getFont(6, ""));
        // Paragraph text05 = PdfFontUtil.getFont(5, "《根据中华人民共和国合同法》的有关规定，经甲、乙双方友好协商，本着长期平等合作.....吧啦吧啦吧啦吧啦吧啦吧啦吧啦吧啦");
        // doc.add(text05);
        //TODO 获取试卷的时候要将题号按照顺序放入
        //题号
        int num = 1;
        //单选题
        if (structure.getSingleChoiceNum() > 0) {
            doc.add(PdfFontUtil.getFont(4, "单选题 (共" + structure.getSingleChoiceScore() + "分)"));
            List<SingleChoice> singles = paperDto.getSingleChoices();
            for (int i = 0; i < structure.getSingleChoiceNum(); i++) {
                SingleChoice single = singles.get(i);
                doc.add(PdfFontUtil.getFont(5, num + ". " + single.getContent()));
                doc.add(PdfFontUtil.getFont(5, "A. " + single.getOptiona()));
                doc.add(PdfFontUtil.getFont(5, "B. " + single.getOptionb()));
                doc.add(PdfFontUtil.getFont(5, "C. " + single.getOptionc()));
                doc.add(PdfFontUtil.getFont(5, "D. " + single.getOptiond()));
                num++;
            }
        }
        //多选题
        if (structure.getMultipleChoiceNum() > 0) {
            doc.add(PdfFontUtil.getFont(4, "多选题 (共" + structure.getMultipleChoiceScore() + "分)"));
            List<MultipleChoice> multipleChoices = paperDto.getMultipleChoices();
            for (int i = 0; i < structure.getSingleChoiceNum(); i++) {
                MultipleChoice multiple = multipleChoices.get(i);
                doc.add(PdfFontUtil.getFont(5, num + ". " + multiple.getContent()));
                doc.add(PdfFontUtil.getFont(5, "A. " + multiple.getOptiona()));
                doc.add(PdfFontUtil.getFont(5, "B. " + multiple.getOptionb()));
                doc.add(PdfFontUtil.getFont(5, "C. " + multiple.getOptionc()));
                doc.add(PdfFontUtil.getFont(5, "D. " + multiple.getOptiond()));
                num++;
            }
        }
        //判断题
        if (structure.getTrueOrFalseNum() > 0) {
            doc.add(PdfFontUtil.getFont(4, "判断题 (共" + structure.getTrueOrFalseScore() + "分)"));
            List<TrueOrFalse> trueOrfalses = paperDto.getTrueOrFalses();
            for (int i = 0; i < structure.getSingleChoiceNum(); i++) {
                TrueOrFalse trueOrFalse = trueOrfalses.get(i);
                doc.add(PdfFontUtil.getFont(5, num + ". " + trueOrFalse.getContent()));
                num++;
            }
        } //填空题
        if (structure.getCompletionNum() > 0) {
            doc.add(PdfFontUtil.getFont(4, "填空题 (共" + structure.getCompletionScore() + "分)"));
            List<BlankFilling> blankFillings = paperDto.getBlankFillings();
            for (int i = 0; i < structure.getSingleChoiceNum(); i++) {
                BlankFilling blankFilling = blankFillings.get(i);
                doc.add(PdfFontUtil.getFont(5, num + ". " + blankFilling.getContent()));
                num++;
            }
        } //简答题
        if (structure.getAnswerNum() > 0) {
            doc.add(PdfFontUtil.getFont(4, "简答题 (共" + structure.getAnswerScore() + "分)"));
            List<ShortAnswer> shortAnswers = paperDto.getShortAnswers();
            for (int i = 0; i < structure.getSingleChoiceNum(); i++) {
                ShortAnswer shortAnswer = shortAnswers.get(i);
                doc.add(PdfFontUtil.getFont(5, num + ". " + shortAnswer.getContent()));
                doc.add(PdfFontUtil.getFont(5, ""));
                doc.add(PdfFontUtil.getFont(5, ""));
                doc.add(PdfFontUtil.getFont(5, ""));
                doc.add(PdfFontUtil.getFont(5, ""));
                doc.add(PdfFontUtil.getFont(5, ""));
                doc.add(PdfFontUtil.getFont(5, ""));
                num++;
            }
        }

        //TODO 整理


        //三、其他
      /*  doc.add(PdfFontUtil.getFont(2, "三、其他"));
        doc.add(PdfFontUtil.getFont(5, "3.自定义 "));

        PdfPTable table = new PdfPTable(2);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "甲方：（盖章）")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "乙方：（盖章）")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "法定代表人或负责人签章")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "法定代表人或负责人签章")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "地址：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "地址：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "开户银行：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "开户银行：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "邮编：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "邮编：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "授权代理人：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "项目经理：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "电话：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell.setBorder(Rectangle.NO_BORDER);
        cell = new PdfPCell(new Phrase(PdfFontUtil.getFont(5, "电话：")));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        doc.add(table);*/
    }
}
